from OpenGL.GL import *
from OpenGL.GLU import *

num_faces = 6

vertices = [ (0.0, 0.0, 1.0),
             (1.0, 0.0, 1.0),
             (1.0, 1.0, 1.0),
             (0.0, 1.0, 1.0),
             (0.0, 0.0, 0.0),
             (1.0, 0.0, 0.0),
             (1.0, 1.0, 0.0),
             (0.0, 1.0, 0.0) ]

normals = [ (0.0, 0.0, +1.0),  # front
            (0.0, 0.0, -1.0),  # back
            (+1.0, 0.0, 0.0),  # right
            (-1.0, 0.0, 0.0),  # left
            (0.0, +1.0, 0.0),  # top
            (0.0, -1.0, 0.0) ] # bottom

vertex_indices = [ (0, 1, 2, 3),  # front
                   (4, 5, 6, 7),  # back
                   (1, 5, 6, 2),  # right
                   (0, 4, 7, 3),  # left
                   (3, 2, 6, 7),  # top
                   (0, 1, 5, 4) ] # bottom

class TerrainRenderer:
    def __init__(self, client):
        self.client = client
        self.render_list = None
        self.first_render = False


    def init(self):
         if self.render_list is None:

            # Create a display list
            self.render_list = glGenLists(1)
            glNewList(self.render_list, GL_COMPILE)

#            glBegin(GL_QUADS)
#
#            for face_no in xrange(num_faces):#{
#
#                glNormal3dv(normals[face_no] )
#
#                v1, v2, v3, v4 = vertex_indices[face_no]
#
#                glVertex( vertices[v1] )
#                glVertex( vertices[v2] )
#                glVertex( vertices[v3] )
#                glVertex( vertices[v4] )
#
#            glEnd()
            

            for face_no in xrange(num_faces):#{
                glBegin(GL_LINE_LOOP)
                #glNormal3dv(normals[face_no] )

                v1, v2, v3, v4 = vertex_indices[face_no]

                glVertex( vertices[v1] )
                glVertex( vertices[v2] )
                glVertex( vertices[v3] )
                glVertex( vertices[v4] )
                glEnd()
            
            
            # End the display list
            glEndList()

    def render(self):
        """
        Render the terrain starting from the cell that the player is currently
        in.
        """

        scale = 10000;
        glScaled(scale,scale,scale)

#         Center the world around the coord in the model
        for d in xrange(2,7):
            if(self.first_render): print "Centering ",self.client.model.coord[d*3],self.client.model.coord[d*3+1],self.client.model.coord[d*3+2]
            glTranslate(-self.client.model.coord[d*3],-self.client.model.coord[d*3+1],-self.client.model.coord[d*3+2])
            glScaled(0.1,0.1,0.1)
        for d in xrange(2,7):
            glScaled(10,10,10)

        #Render every loaded block from largest to smallest

        
        
        glLineWidth(2)
        glColor3d(0.9,0,0)
        #glScaled(scale,scale,scale)
        #scale = 1
#        for d in xrange(1,6):
#            cell = self.client.model.roots[d]
#            glTranslate(cell.x*-scale,cell.y*-scale,cell.z*-scale)
#            scale = scale * 0.1
        cell = self.client.model.roots[2]
        depth_stack = [None]*10
        depth = 0;
        #render_loop = 0
        while( cell != None ):
            glTranslate(cell.x,cell.y,cell.z)
            if(depth == 4):
                glColor3d(0,0.7,0)
                glLineWidth(1)
            #glCallList(self.render_list)
            glCallList(cell.obj.display_list)
            if(depth == 4):
                glLineWidth(2)
                glColor3d(0.9,0,0)
            if(cell.first_child != None):
                if(self.first_render): print "down"
                depth_stack[depth] = cell
                depth += 1
                glScaled(0.1,0.1,0.1)
                cell = cell.first_child
                continue
            elif (cell.next != None):
                if(cell.terrain == 0):
                    glColor3d(0.9,0.7,0.5)
                else:
                    glColor3d(0.5,0.5,0.9)
                if(self.first_render): print "next"
                glTranslate(-cell.x,-cell.y,-cell.z) # "reset" the sub coordinate system before rendering the next cell
                cell = cell.next
            else:
                glTranslate(-cell.x,-cell.y,-cell.z) 
                while(cell.next is None):
                    if(self.first_render): print "up"
                    depth -= 1
                    if(depth < 0): break
                    cell = depth_stack[depth]
                    if(cell is None): break
                    glScaled(10,10,10)
                    glTranslate(-cell.x,-cell.y,-cell.z)
                cell = cell.next

            #render_loop +=1


#            glScaled(0.1,0.1,0.1)
            

            
#            if(self.first_render):
#                    print "Drawing Root ", d, " xyz (", cell.x,",",cell.y,",",cell.z, ") (",cell.count,")" , cell.prev, cell.next
#
#            glScaled(0.1,0.1,0.1)
#            glTranslate(-cell.x,-cell.y,-cell.z)
#            while(cell != None):
#
#                if(self.first_render):
#                    print "Drawing (", cell.x,",",cell.y,",",cell.z, ")"
#
#                glTranslate(cell.x,cell.y,cell.z)
#                glColor3d(0.9,0,0)
#                glCallList(self.render_list)
#                for i in cell.entities:
#                    i.draw()
#                glTranslate(-cell.x,-cell.y,-cell.z)
#
#                cell = cell.next
#            cell = self.client.model.roots[d]
#            glTranslate(cell.x,cell.y,cell.z)
            #cell = self.client.model.roots[d]
            
            
    #            glCallList(self.render_list)
                #scale -= 10
#                cell = cell.first_child
#                while(cell != None):
#                    if(self.first_render):
#                        print "Drawing Root Sub ", d, " xyz (", cell.x,",",cell.y,",",cell.z, ") at scale ( ", scale,") (",cell.count,")"
#
#
#    #                if(cell.terrain == 0):
#    #                    cell = cell.next
#    #                    continue
#
#                    glTranslate(cell.x,cell.y,cell.z)
#                    glCallList(self.render_list)
#                    glTranslate(-cell.x,-cell.y,-cell.z)
#                    cell = cell.next

#                cell = self.client.model.roots[d].first_child
#                while(cell != None):
#    #                if(self.first_render):
#    #                    print "Drawing Root Sub ", d, " xyz (", cell.x,",",cell.y,",",cell.z, ") at scale ( ", scale,") (",cell.count,")"
#
#                    if(cell.terrain == 1):
#                        cell = cell.next
#                        continue
#
#                    scell = cell.first_child
#
#                    glTranslate(cell.x,cell.y,cell.z)
#                    glScaled(0.1,0.1,0.1)
#                    while(scell != None):
#
#                        if(scell.terrain == 0):
#                            scell = scell.next
#                            continue
#
#                        glTranslate(scell.x,scell.y,scell.z)
#                        glCallList(self.render_list)
#                        glTranslate(-scell.x,-scell.y,-scell.z)
#                        scell = scell.next
#                    glScaled(10,10,10)
#                    glTranslate(-cell.x,-cell.y,-cell.z)
#                    cell = cell.next
#        print "rendered "
#        print count


        self.first_render = False

   